import Vue from "vue";
/**
 * 首字母大写
 * @param str 字符串
 * @example heheHaha
 * @return {string} HeheHaha
 */
function capitalizeFirstLetter(str) {
  return str.charAt(0).toUpperCase() + str.slice(1);
}

/**
 * 对符合'xx/xx.vue'组件格式的组件取组件名
 * @param str fileName
 * @example abc/bcd/def/basicTable.vue
 * @return {string} BasicTable
 */
function validateFileName(str) {
  return (
    /^\S+\.vue$/.test(str) &&
    str.replace(/^\S+\/(\w+)\.vue$/, (rs, $1) => capitalizeFirstLetter($1))
  );
}
/**
 * @param {String} directory -读取文件的路径
 * @param {Boolean} useSubdirectories -是否遍历文件的子目录
 * @param {regExp} regExp -匹配文件的正则
 */
const requireComponent = require.context(".", true, /\.vue$/);
/**
    console.log(requireComponent)
    ƒ webpackContext(req) {
    var id = webpackContextResolve(req);
    return __webpack_require__(id);
    }
* */
// 找到组件文件夹下以.vue命名的文件，如果文件名为index，那么取组件中的name作为注册的组件名
requireComponent.keys().forEach(filePath => {
  // console.log(filePath) // ./Breadcrumb/index.vue  ./Cascader/index.vue ....
  const componentConfig = requireComponent(filePath);
  const fileName = validateFileName(filePath);
  // console.log(componentConfig,'componentConfig')
  /**
     * Module{
     *  default:{
     *          beforeCreate:Array[1]
     *          beforeDestroy:Array[1],
     *          created: ƒ created()
                data: ƒ data()
     *          .....
     *          methods:{}
                name: "Breadcrumb"
     *     }
     * }
     */
  const componentName =
    fileName.toLowerCase() === "index"
      ? capitalizeFirstLetter(componentConfig.default.name)
      : fileName;
  Vue.component(componentName, componentConfig.default || componentConfig);
});
